package com.aube.interact.fighter;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aube.interact.fighter.roundedimageview.RoundedImageView;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.dynamicload.video.model.GoodsModel;
import com.huyn.baseframework.dynamicload.video.model.GoodsRoute;
import com.huyn.baseframework.utils.Utils;

import java.util.List;

/**
 * Created by hill on 16/11/8.
 */

public class SmallGoodsItem extends RelativeLayout {
    private String TAG = "SmallGoodsItem";

    private Paint mPaint;

    private Bitmap bitmap;
    private Shader shader;

    private Paint mBorderPaint, mTopPaint;

    private Context mContext;

    private GoodsModel mGoodsModel;

    private TextView mGoodsTitle;
    private RoundedImageView mGoodsImg;

    private String mImgUrl;

    private float mAlpha = 1.0f;

    private IGoodsDetailRevealAnimator mAnimator;

    public SmallGoodsItem(Context context) {
        this(context, null);
    }

    public SmallGoodsItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SmallGoodsItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;

        mPaint = new Paint();
        mPaint.setAntiAlias(true);

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.stripe);

        shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        mPaint.setShader(shader);

        mBorderPaint = new Paint();
        mBorderPaint.setColor(Color.BLACK);
        mBorderPaint.setAntiAlias(true);

        mTopPaint = new Paint();
        mTopPaint.setColor(Color.WHITE);
        mTopPaint.setAntiAlias(true);

        initView();
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.aube_intera_layout_smallgoodsitem, this);

        mGoodsTitle = (TextView) findViewById(R.id.smallgoodsitem_txt);
        mGoodsImg = (RoundedImageView) findViewById(R.id.smallgoodsitem_img);

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mAnimator != null) {
                    // fill data
                    mAnimator.prepareData(mGoodsModel);

                    // finally, setup view & pre anim layout
                    int[] location = new int[2];
                    mGoodsImg.getLocationInWindow(location);
                    int size = mGoodsImg.getWidth();
                    mAnimator.prepareAnimFromGoodsLabel(size, location[0], location[1], new GoodsDetailLayout.IAnimEndListener() {
                        @Override
                        public void onAnimEnd() {
                            reverse();
                        }

                        @Override
                        public void onStartFinishAnim() {
                            //hidebg
                            mAnimator.hideBg();
                        }
                    });

                    //hide text
                    ValueAnimator anim = ValueAnimator.ofFloat(1f, 0f);
                    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            float value = (float) animation.getAnimatedValue();
                            mAlpha = value;
                            mGoodsTitle.setAlpha(value);
                            invalidate();
                        }
                    });
                    anim.setDuration(300);
                    anim.addListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            mAnimator.startAnimFromGoodsLabel();

                            //showbg
                            mAnimator.showBg();

                            setVisibility(GONE);
                        }
                    });
                    anim.start();
                }
            }
        });
    }

    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        drawBg(canvas);
        drawBorder(canvas);
        return super.drawChild(canvas, child, drawingTime);
    }

    private void drawBg(Canvas canvas) {
        mPaint.setColor(Color.RED);
        mPaint.setAlpha((int) (mAlpha*255));
        Path path = new Path();
        path.addCircle(getHeight()/2, getHeight()/2, getHeight()/2, Path.Direction.CW);
        RectF rectF = new RectF(getHeight()/2, getHeight()/4, getWidth(), getHeight()*3/4);
        path.addRoundRect(rectF, 10, 10, Path.Direction.CW);
        canvas.drawPath(path, mPaint);
    }

    private void drawBorder(Canvas canvas) {
        mBorderPaint.setAlpha((int) (mAlpha*255));
        canvas.drawCircle(getHeight()/2, getHeight()/2, getHeight()/2 - 10, mBorderPaint);
        RectF rectF = new RectF(getHeight()/2, getHeight()/4+10, getWidth()-10, getHeight()*3/4-10);
        canvas.drawRoundRect(rectF, 5, 5, mBorderPaint);

        mTopPaint.setAlpha((int) (mAlpha*255));
        canvas.drawCircle(getHeight()/2, getHeight()/2, getHeight()/2 - 12, mTopPaint);
        RectF rectF2 = new RectF(getHeight()/2, getHeight()/4+12, getWidth()-12, getHeight()*3/4-12);
        canvas.drawRoundRect(rectF2, 4, 4, mTopPaint);
    }

    public void addGoodsAnimListener(IGoodsDetailRevealAnimator animator) {
        mAnimator = animator;
    }

    public void fillData(GoodsModel data) {
        mGoodsModel = data;
        if(data.goodsVo == null)
            return;
        mImgUrl = data.goodsVo.getPic();

        mGoodsTitle.setText(data.goodsVo.tlTitle);
        ImageLoader.getInstance(getContext()).fillImage(mGoodsImg, mImgUrl, 600, 600);
    }

    /**
     * 从详情回到标签模式
     */
    public void reverse() {
        setVisibility(View.VISIBLE);
        ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mAlpha = value;
                mGoodsTitle.setAlpha(value);
                invalidate();
            }
        });
        anim.setDuration(300);
        anim.start();
    }

    /**
     * 商品图片渐现的动画, 渐现完毕, 商品名字从左往右横移动画
     */
    public void startAlphaAnim() {
        Utils.Log(Utils.LogType.DEBUG, TAG, "startAlphaAnim");
        mGoodsTitle.setAlpha(0f);
        mGoodsTitle.setVisibility(VISIBLE);
        ValueAnimator anim = ValueAnimator.ofFloat(0.0f, 1.0f);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animValue = (float) animation.getAnimatedValue();

                mGoodsImg.setAlpha(animValue);
            }
        });

        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                startGoodsTextExtendAnim();
            }
        });

        anim.setDuration(300);
        anim.start();

        Utils.Log(Utils.LogType.DEBUG, TAG, "startAlphaAnim done");
    }


    /**
     * 商品名字从做至右的动画
     */
    private void startGoodsTextExtendAnim() {
        ValueAnimator anim = ValueAnimator.ofFloat(0.0f, 1.0f);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mAlpha = value;
                mGoodsTitle.setAlpha(value);
                invalidate();
            }
        });
        anim.setDuration(400);
        anim.start();
    }

}
